package com.oceansoft.mobile.econsole.modules.api.util;

import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;
import com.oceansoft.mobile.econsole.modules.member.entity.WtMember;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * Created by xuhb on 2017/3/29.
 */
public class UserInfoUtil {
    public static WtMember getWtMember(String json) {
        WtMember wtMember = new WtMember();
        JSONObject userInfo = JSONObject.parseObject(json);
        // 主键
        wtMember.setGuid(userInfo.getString("guid"));
        // 手机号
        wtMember.setUserName(userInfo.getString("acountId"));
        // 姓名
        wtMember.setAliasName(userInfo.getString("realName"));
        // 身份证号
        wtMember.setUserCode(userInfo.getString("idNum"));


        String province = userInfo.getString("province");
        String city = userInfo.getString("city");
        String area = userInfo.getString("area");
        String address = userInfo.getString("address");
        // 省
        wtMember.setProvince(province);
        // 市
        wtMember.setCity(city);
        // 区
        wtMember.setArea(area);
        // 街道
        wtMember.setStreet(address);

        StringBuilder builder = new StringBuilder();
        if(!Strings.isNullOrEmpty(province)) {
            builder.append(province + "-");
        }
        if(!Strings.isNullOrEmpty(city)) {
            builder.append(city + "-");
        }
        if(!Strings.isNullOrEmpty(area)) {
            builder.append(area + "-");
        }
        if(!Strings.isNullOrEmpty(address)) {
            builder.append(address);
        }
        wtMember.setAddress(builder.toString());

        // 电话
        wtMember.setMobile(userInfo.getString("telephone"));
        // 邮箱
        wtMember.setEmail(userInfo.getString("email"));
        // openid
        wtMember.setWeixinid(userInfo.getString("wxOpenId"));

        // 统一用户状态处理: 0-未发起， 2-认证中 ， 1-通过  ，-1-未过
        String status;
        String authStatus = userInfo.getString("authStatus");// authStatus: 0-未发起，1-已发起， 9-重新发起， 2-已认证
        if (Objects.equals("2", authStatus)) {//已经认证，才去获取认证状态
            status = "2";
            JSONObject memberStatus = userInfo.getJSONObject("memberStatus");
            if (Objects.nonNull(memberStatus)) {
                status = memberStatus.getString("identityStatus");//实名认证状态，1-通过  ，-1-未过
            }
        }else if (Objects.equals("1", authStatus) || Objects.equals("9", authStatus)) {
            status = "2";
        }else {
            status = authStatus;
        }
        wtMember.setIdentifystatus(status);//实名认证状态

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");


        // 注册来源
        wtMember.setZzmm(userInfo.getString("source"));

        Date date=new Date(userInfo.getString("createTime"));
        wtMember.setDateCreated(date);

        return wtMember;
    }
}
